//
//  NeedUpDataViewController.m
//  Bus_iPhone
//
//  Created by 修勇 黄 on 12-7-25.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "NeedUpDataViewController.h"
#import "UpDataBusCell.h"
#import "BusSetting.h"

@interface NeedUpDataViewController ()

@end

@implementation NeedUpDataViewController

@synthesize list=_list;

-(void)dealloc{

    [[[BusSetting defaultSetting]webService]setDelegate:nil];
    [_list release];
    [super dealloc];
}

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [[[BusSetting defaultSetting]webService]setDelegate:self];
    [self setCustomNavigationReturnItem];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return _list.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UpDataBusCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (!cell) {
        cell=[[[UpDataBusCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        [cell.downLoadBtn addTarget:self action:@selector(downLoadBtnDown:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    NSDictionary *dic=[_list objectAtIndex:indexPath.row];
    
    cell.textLabel.text=[dic objectForKey:@"CityName"];
    if ([[[BusSetting defaultSetting]webService]checkIsHadDownByHost:[dic objectForKey:@"URL"]]) {
        cell.state=UpDataBusStateIsDownIng;
        [[[BusSetting defaultSetting]webService]addProgressDelegate:cell.progressView host:[dic objectForKey:@"URL"]];
    }else {
        cell.state=UpDataBusStateIsNotDownLoad;
    }
    // Configure the cell...
    
    return cell;
}


-(void)downLoadBtnDown:(UIButton *)btn{
    
    UpDataBusCell *cell=(UpDataBusCell *)[[btn superview]superview];
    NSIndexPath *indexPath=[self.tableView indexPathForCell:cell];
    NSDictionary *dic=[self.list objectAtIndex:indexPath.row];
    [[[BusSetting defaultSetting]webService]addDwonLoadFileWithHost:[dic objectForKey:@"URL"] fileName:[dic objectForKey:@"CityName"] progress:cell.progressView];
    cell.state=UpDataBusStateIsDownIng;
}


#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   
}

-(void)busFileDownFailed:(ASIHTTPRequest *)downRequest error:(NSError *)error{
    
    NSDictionary *dic=[downRequest userInfo];
    NSString *msg=[NSString stringWithFormat:@"%@ 数据库下载失败!",[dic objectForKey:@"name"]];
    [self showAlertWithTitle:@"失败" msg:msg];
    [self.tableView reloadData];
}

-(void)busFileDownSuccess:(ASIHTTPRequest *)downRequest{

    for (NSDictionary *dic in _list) {
        NSURL *url=[NSURL URLWithString:[dic objectForKey:@"URL"]];
        if ([url isEqual:downRequest.url]) {
            NSMutableArray *tempArray=[[NSMutableArray alloc]initWithArray:_list];
            NSInteger row=[_list indexOfObject:dic];
            [tempArray removeObject:dic];
            self.list=tempArray;
            [tempArray release];
            NSIndexPath *indexPath=[NSIndexPath indexPathForRow:row inSection:0];
            [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
            break;
        }
    }
}

@end
